package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.List;

/**
 * <h3>health94_pareant</h3>
 * <p></p>
 *
 * @author : hhw
 * @date : 2020-03-21 16:58
 **/
@Service(interfaceClass = SetmealService.class)
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public List<CheckGroup> queryCheckGroup() {

        return setmealMapper.queryCheckGroup();

    }

    @Override
    public void add(Integer[] checkgroupIds, Setmeal setmeal) {

        //要记得主键回显
        setmealMapper.addSetmeal(setmeal);

        //在图片上传至数据库成功之后将图片存入redis中一份
        if (setmeal.getImg() != null) jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());

        setmealMapper.addSetmealRelateCheckgroup(checkgroupIds, setmeal.getId());

    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        //使用分页插件
        PageHelper.startPage(currentPage, pageSize);

        List<Setmeal> list = setmealMapper.findPage(queryString);
        PageInfo<Setmeal> pageInfo = new PageInfo<>(list);
        return new PageResult(pageInfo.getTotal(), list);


    }

    @Override
    public List<Setmeal> queryAllsetmeal() {

        return setmealMapper.queryAllsetmeal();


    }

    @Override
    public Setmeal querySetmealDetailById(Integer id) {
        //单sql语句查询法
        return setmealMapper.querySetmealDetailById(id);




    }

    @Override
    public Setmeal setmealInfo(Integer id) {
        return setmealMapper.setmealInfo(id);
    }
}
